package com.example.xd.model_permission;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;


/**
 * @auther XuDan on 2019/5/27.
 */
public class NotificationsUtils {

    public static boolean isNotificationEnabled(Context context) {
        return NotificationManagerCompat.from(context).areNotificationsEnabled();
    }

    public static void getNotifictionPermission(Context context) {
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();

        View view = View.inflate(context, R.layout.permission_dialog, null);
        dialog.setContentView(view);

        TextView textView = (TextView) view.findViewById(R.id.tv_dialog_context);
        textView.setText("检测到您没有打开通知权限，是否去打开");

        TextView confirm = (TextView) view.findViewById(R.id.btn_confirm);
        confirm.setText("确定");
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra("app_package", context.getPackageName());
                intent.putExtra("app_uid", context.getApplicationInfo().uid);
                context.startActivity(intent);
            }
        });

        TextView cancel = (TextView) view.findViewById(R.id.btn_off);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    }
}
